﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using Microsoft.Office;
//using Microsoft.Vbe.Interop;
//using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace jnagr.Web
{
    public class ExWord
    {
        public static string ExportToWordExped(string areas, string jbdata)
        {
            object outname;
            //创建WordApp对象
            string filename = "";
            Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            try
            {
                GC.Collect();
                object missing = System.Reflection.Missing.Value;
                object Nothing = System.Reflection.Missing.Value;


                //创建一个名为WordDoc的文档对象               
                Microsoft.Office.Interop.Word.Document doc;
                object b = System.Web.HttpContext.Current.Server.MapPath("~/template/e.doc");
                doc = WordApp.Documents.Open(ref b, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                find("@nowdate@", DateTime.Now.ToShortDateString(), Nothing, WordApp);
                find("@jbdata@", jbdata, Nothing, WordApp);
                find("@areas@", areas, Nothing, WordApp);
                string ticks = DateTime.Now.Ticks.ToString();
                outname = System.Web.HttpContext.Current.Server.MapPath("~/out/") +ticks + "e.doc";
                if (File.Exists(outname.ToString()))
                    File.Delete(outname.ToString());
                doc.SaveAs(ref outname, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                doc.Close(ref Nothing, ref Nothing, ref Nothing);
                filename = "/out/" + ticks + "e.doc";
                //关闭WordApp对象
                WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                WordApp = null;
                doc = null;
                KillWord();
                GC.Collect();



            }
            catch (Exception ex)
            {
                KillWord();
                throw ex;
            }
            return filename;
        }

        private static void find(object str, object replacestr, object Nothing, Microsoft.Office.Interop.Word.Application WordApp)
        {
            WordApp.Selection.WholeStory();

            Find find = WordApp.Selection.Find; ;
            find.ClearFormatting();

            //object str = "school";
            //object replacestr = ds.Tables[0].Rows[0]["school"].ToString();
            find.Execute(ref str, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref replacestr, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);


        }


        private static void KillWord()
        {
            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD"))
            {
                p.Kill();
            }
        }
    }
}
